package com.ruoyi.project.data.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.data.domain.WeightEquipment;
import com.ruoyi.project.data.service.IWeightEquipmentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2020-03-21
 */
@RestController
@RequestMapping("/system/equipment")
public class WeightEquipmentController extends BaseController
{
    @Autowired
    private IWeightEquipmentService weightEquipmentService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(WeightEquipment weightEquipment)
    {
        // 判断是否是场站角色
        if(permissionService.hasRole("admin")){    //管理员

        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station") || permissionService.hasRole("xq_station")
                || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station")){     //场站
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            weightEquipment.setStationId(stationId);
        }
        startPage();
        List<WeightEquipment> list = weightEquipmentService.selectWeightEquipmentList(weightEquipment);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WeightEquipment weightEquipment)
    {
        // 判断是否是场站角色
        if(permissionService.hasRole("admin")){    //管理员

        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station") || permissionService.hasRole("xq_station")
                || permissionService.hasRole("jy_station") || permissionService.hasRole("yx_station")){     //场站
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            weightEquipment.setStationId(stationId);
        }
        List<WeightEquipment> list = weightEquipmentService.selectWeightEquipmentList(weightEquipment);
        ExcelUtil<WeightEquipment> util = new ExcelUtil<WeightEquipment>(WeightEquipment.class);
        return util.exportExcel(list, "equipment");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(weightEquipmentService.selectWeightEquipmentById(id));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeightEquipment weightEquipment)
    {
        if (UserConstants.NOT_UNIQUE.equals(weightEquipmentService.checkEquipmentCodeUnique(weightEquipment)))
        {
            return AjaxResult.error("新增设备失败，设备编号已存在");
        }
        return toAjax(weightEquipmentService.insertWeightEquipment(weightEquipment));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeightEquipment weightEquipment)
    {
        if (UserConstants.NOT_UNIQUE.equals(weightEquipmentService.checkEquipmentCodeUnique(weightEquipment)))
        {
            return AjaxResult.error("编辑设备失败，设备编号已存在");
        }
        return toAjax(weightEquipmentService.updateWeightEquipment(weightEquipment));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('weight:equipment:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(weightEquipmentService.deleteWeightEquipmentByIds(ids));
    }
}
